<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-row type="flex" justify="end" align="middle" style="height: 60px">
          <el-button type="primary" plain @click="addPerm('0','1')">添加权限</el-button>
        </el-row>
        <el-divider />
        <el-table
          :data="tableData"
          border
          style="width: 100%"
          row-key="id"
        >
          <el-table-column
            prop="title"
            label="权限名称"
            align="center"
          />
          <el-table-column
            prop="code"
            label="权限标识"
            align="center"
          />
          <el-table-column
            prop="description"
            label="权限描述"
            align="center"
          />
          <el-table-column
            label="操作"
            align="center"
            width="280"
          >
            <template v-slot="{row}">
              <el-button type="warning" plain size="small" @click="addPerm(row.id,'2')">添加子权限</el-button>
              <el-button type="primary" plain size="small" @click="editFn(row.id)">编辑</el-button>
              <el-button type="danger" plain size="small" @click="delFn(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <el-dialog :title="formData.id?'编辑权限':'添加权限'" :visible="showDialog" @close="closeFn">
        <el-form ref="form" label-width="120px" :model="formData" :rules="rules">
          <el-form-item label="权限名称" prop="title">
            <el-input v-model="formData.title" style="width:80%" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" style="width:80%" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" style="width:80%" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <template v-slot:footer>
          <el-button size="small" plain @click="closeFn">取 消</el-button>
          <el-button size="small" type="primary" plain @click="confirmFn">确 定</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionsList, addPermission, editPermission, getPermission, delPermission } from '@/api/permissions'
import { listToTree } from '@/utils/index'
export default {
  data() {
    return {
      tableData: [],
      showDialog: false,
      formData: {
        enVisible: 1, // 开启 标识是否启用这个权限 ，1表示启用
        title: '',
        code: '',
        description: '',
        type: '1', // 1表示页面访问权限 2表示按钮操作权限
        pid: '0' // pid值为0时表示添加页面访问权限，值为其他的字符串时表示添加按钮操作权限
      },
      rules: {
        title: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '权限描述不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getPermissionsList()
  },
  methods: {
    async getPermissionsList() {
      const res = await getPermissionsList()
      // console.log(56, res.data)
      // this.tableData = res.data
      this.tableData = listToTree(res.data, '0')
      // console.log(this.tableData)
      // console.log(this.tableData, res.data)
    },
    addPerm(pid, type) {
      // console.log(111)
      this.showDialog = true
      this.formData.pid = pid.toString()
      // console.log('110', this.formData.pid)
      this.formData.type = type
    },
    async editFn(id) {
      // console.log(222)
      this.showDialog = true
      const res = await getPermission(id)
      // console.log(110, res)
      this.formData = res.data
    },
    closeFn() {
      this.$refs.form.resetFields()
      this.showDialog = false
      this.formData = {
        enVisible: 1, // 开启 标识是否启用这个权限 ，1表示启用
        title: '',
        code: '',
        description: '',
        type: '1', // 1表示页面访问权限 2表示按钮操作权限
        pid: '' // pid值为0时表示添加页面访问权限，值为其他的字符串时表示添加按钮操作权限
      }
    },
    async confirmFn() {
      if (this.formData.id) {
        await editPermission(this.formData)
      } else {
        await addPermission(this.formData)
      }
      await this.$refs.form.validate()
      this.getPermissionsList()
      this.closeFn()
    },
    delFn(id) {
      this.$confirm('确定删除此权限点吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPermission(id)
        // if (this.tableData.length === 1 && this.curPage !== 1) {
        //   this.curPage--
        // }
        this.getPermissionsList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style>

</style>
